<?php
/**
 * ClassName:执行控制器
 * @auditor:leijunxiang
 * @time 2025/4/8 13:45
 */

namespace app\tool\word;

use app\model\word\DataPosition;
use app\model\word\TableData;
use app\model\word\TitlePosition;
use app\tool\ToolBase;
use think\facade\Db;
use think\facade\Log;

class ExecuteControllerHandle extends ToolBase
{
    //标题定位对象
    protected $titlePositionObj;
    //excel对象
    protected $excelObj;
    //数据定位对象
    protected $dataPositionObj;
    //表格数据对象
    protected $tableDataObj;
    //开始执行(暂时没用)
    protected $startAttr;

    /**
     * (开始数组)开始执行(行，列，数据，excel对象,表格数据对象,货代id,开始标识,行结束位移)
     */
    public function startExcel($row, $col, $data, $excel, $tableData, $forwardId,$startFlag, $rowOffset)
    {
        $arr = [];
        // 启动事务
        Db::startTrans();
        try {
            //初始化数据
            $this->initData($data, $excel,$tableData,$forwardId,$startFlag, $rowOffset);
            //数据填充(行，列,数据)
            $this->dataFill($row, $col, $data);
            //校验数据是否填充完毕
            $this->verifyData();
            //标题处理
            $this->titleHandle($forwardId);
            //参数处理
            $this->paramHandle();
            //设置开始数组
            $this->setStartAttr();
            $arr = $this->startAttr;
            //清除数据
            $this->cleanData();
            if (!$this->isGoOn()) {
                // 回滚事务
                Db::rollback();
            }else{
                // 提交事务
                Db::commit();
            }
        } catch (\Exception $e) {
            $this->errCode = FAIL;
            $this->errMsg = $e->getMessage();
            // 回滚事务
            Db::rollback();
        }
        return $arr;
    }

    /**
     * 初始化数据
     */
    protected function initData($data, $excel,$tableData,$forwardId,$startFlag, $rowOffset)
    {
        $this->init();
        //excel对象
        if (empty($this->excelObj)) {
            $this->excelObj = $excel;
        }
        //标题定位对象
        if (empty($this->titlePositionObj)) {
            $this->titlePositionObj = app(titlePosition::class);
        }

        //数据定位对象
        if (empty($this->dataPositionObj)) {
            $this->dataPositionObj = app(dataPosition::class);
        }

        //表格数据对象
        if (empty($this->tableDataObj)) {
            $this->tableDataObj = $tableData;
        }
        //开始执行
        $this->startAttr = [];
        //货代id
	    $this->tableDataObj->setForwardId($forwardId);
        //开始标识
        $this->tableDataObj->setStartFlag($startFlag);
        //行结束位移
        $this->tableDataObj->setRowOffset($rowOffset);
    }

    /**
     * 数据填充(行，列,数据)
     */
    protected function dataFill($row, $col, $data)
    {
        if (!$this->isGoOn()) {
            return;
        }
        $handle = app(DataFillHandle::class);
        //(true/false)开始填充(标题定位对象,数据定位对象,表格数据对象,excel对象,行,列)
        $flag = $handle->startFill($this->titlePositionObj, $this->dataPositionObj, $this->tableDataObj, $this->excelObj, $row, $col);
        if (!$flag) {
            $this->errCode = $handle->getErrCode();
            $this->errMsg = $handle->getErrMsg();
        }
    }

    /**
     * 校验数据是否填充完毕
     */
    protected function verifyData()
    {
        if (!$this->isGoOn()) {
            return;
        }
        if (empty($this->titlePositionObj->getChannelCoord())
            || empty($this->titlePositionObj->getAreaCoord())
            || empty($this->titlePositionObj->getWeightStartCoord())
            || empty($this->titlePositionObj->getWeightEndCoord())
            || empty($this->dataPositionObj->getLeftTopCoord())
            || empty($this->dataPositionObj->getRightTopCoord())
        ) {
            $this->errCode = WAIT;
            $this->errMsg = "数据未填充完毕,暂不执行";
        } else {
            $this->errCode = SUCCESS;
            $this->errMsg = "数据填充完毕,开始执行";
        }
    }

    /**
     * 标题处理
     */
    protected function titleHandle($forwardId)
    {
        if (!$this->isGoOn()) {
            return;
        }
        //标题处理
        $handle = app(TitleHandle::class);
        //(true/false)开始处理(标题定位对象,excel对象,表格数据对象,货代id)
        $flag = $handle->startHandle($this->titlePositionObj, $this->excelObj, $this->tableDataObj, $forwardId);
        if (!$flag) {
            $this->errCode = $handle->getErrCode();
            $this->errMsg = $handle->getErrMsg();
        }
    }

    /**
     * 参数处理
     */
    protected function paramHandle()
    {
        if (!$this->isGoOn()) {
            return;
        }
        //参数处理器
        $handle = app(ParamHandle::class);
        //(true/false)开始处理(数据定位对象,excel对象，表格数据对象)
        $flag = $handle->startHandle($this->dataPositionObj, $this->excelObj, $this->tableDataObj);
        if (!$flag) {
            $this->errCode = $handle->getErrCode();
            $this->errMsg = $handle->getErrMsg();
            return;
        }
    }

    /**
     * 设置开始数组
     */
    protected function setStartAttr()
    {
        if (!$this->isGoOn()) {
            return;
        }
        //开始行
        $row = $this->dataPositionObj->getRightTopCoord()[0] + 1;
        //开始列
        $col = "A";
        $this->startAttr = [
            $row,
            $col
        ];
    }


    /**
     * 清除数据
     */
    protected function cleanData()
    {
        //渠道坐标
        $this->titlePositionObj->setChannelCoord([]);
        //区域坐标
        $this->titlePositionObj->setAreaCoord([]);
        //重量开始坐标
        $this->titlePositionObj->setWeightStartCoord([]);
        //重量结束坐标
        $this->titlePositionObj->setWeightEndCoord([]);

        //左上角坐标
        $this->dataPositionObj->setLeftTopCoord([]);
        //右下角坐标
        $this->dataPositionObj->setRightTopCoord([]);

        //渠道id
	    $this->tableDataObj->setChannelId("");
        //货代id
//	    $this->tableDataObj->setForwardId("");
        //货代名称
	    $this->tableDataObj->setName("");
        //渠道名称
	    $this->tableDataObj->setChannelName("");
        //区域名称
	    $this->tableDataObj->setAreaName("");
        //重量队列
	    $this->tableDataObj->setWeightList("");
        //价格队列
	    $this->tableDataObj->setPriceList("");


        //标题定位对象
        $this->titlePositionObj = null;
        //excel对象
        $this->excelObj = null;
        //数据定位对象
        $this->dataPositionObj = null;
        //表格数据对象
        $this->tableDataObj = null;
        $this->startAttr = [];
    }


    /**
     * (true/false)校验字符串(字符串,检验字符)
     */
    protected function verifyString($string, $char)
    {
        if (empty($string) || empty($char)) {
            return false;
        }
        // 使用 strpos 函数检查 $char 是否存在于 $string 中
        return strpos($string, $char) !== false;
    }


}